package org.neuedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.neuedu.api.FileUploadService;
import org.neuedu.common.ResultJson;
import org.neuedu.entity.Employee;
import org.neuedu.service.DepartmentService;
import org.neuedu.service.EmployeeService;
import org.neuedu.service.RegistLevelService;
import org.neuedu.service.SchedulingService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2024-11-07
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    EmployeeService employeeService;
    @Resource
    DepartmentService departmentService;
    @Resource
    RegistLevelService registLevelService;
    @Resource
    SchedulingService schedulingService;
    @Resource
    FileUploadService fileUploadService;
    @Resource(name="lzwredis")
    RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/getEditData")
    ResultJson<Map<String, List<?>>> getEditData () {
        Map<String, List<?>> map = new HashMap<>();
        map.put("depts", departmentService.getActive());
        map.put("regists", registLevelService.getActive());
        map.put("schedulings", schedulingService.getActive());
        return ResultJson.success(map);
    }

    @PostMapping("/del")
    ResultJson<Boolean> del(Long id,Boolean delmark) {
        return ResultJson.success(employeeService.del(id, delmark), delmark?"还原员工成功":"删除员工成功");
    }

    @GetMapping("/list")
    ResultJson<IPage<Employee>> list(Integer pageNo, Integer pageSize, String name) {
        return ResultJson.success(employeeService.list(pageNo, pageSize, name));
    }
    @GetMapping("/check")
    ResultJson<Boolean> chcek(String field,String value,Long id){
        return ResultJson.success(employeeService.check(field,value,id));
    }
    @PostMapping("/add")
    ResultJson<Boolean> add(Long deptmentId, Long registLevelId, Long schedulingId, String realname, String phone, String email, String password, MultipartFile file){
        employeeService.add(deptmentId, registLevelId, schedulingId, realname, phone, email, password, file);
        return ResultJson.success(true, "添加员工成功");
    }
    @GetMapping("/getRegisterData")
    List<Employee> getRegisterData(Integer week,String noon,Long deptId,Long registLevelId){
        return employeeService.getRegisterEmp(week,noon,deptId,registLevelId);
    }

    @GetMapping("/getRegisterEmp")
    ResultJson<List<Employee>> getRegisterEmp(Integer week,String noon,Long deptId,Long registLevelId){
        return ResultJson.success(employeeService.getRegisterEmp(week,noon,deptId,registLevelId));
    }

    @PostMapping("/login")
    ResultJson<String> login(String phone,String password){
        return ResultJson.success(employeeService.login(phone,password));
    }
    @PostMapping("/logout")
    ResultJson<Boolean> loginout(String phone,String password){
        return ResultJson.success(employeeService.loginout(phone));
    }
    @GetMapping("/getByPhone")
    ResultJson<Employee> getByPhone(String phone){
        return ResultJson.success(employeeService.getByPhone(phone));
    }
    @GetMapping ("/getNameById")
    String getNameById(Long id){
        return employeeService.getById(id).getRealname();
    }
    @PostMapping("/update")
    ResultJson<Boolean> updateEmpById(Employee data,String oldpassword,MultipartFile file){
        if (employeeService.updateEmpById(data,oldpassword,file)){
            return ResultJson.success(true,"修改成功");
        }else{
            return ResultJson.failed("旧密码错误");
        }
    }

}
